<?php
namespace Admin\Controller;
use Think\Controller;
header("Content-Type:text/html; charset=utf-8");
class CourseController extends Controller {


    public function _initialize()
    {
        if(!check_login()){
            $this->error('登录状态异常,请重新登录','/Admin/Login/login');
        }
    }


    //产品列表
    public function index()
    {
        $page   =   I("get.page");
        $cate_id    =   I("get.cate_id");
        if(empty($page))    $page   =   0;
        $ids    =   D("Common")->getCateIds('pro_cate',$cate_id);
        $keywords   =   I("get.keywords");
        if($ids)    $sql    =   "cate_id in ({$ids})";
        else $sql   =   '';
        if($keywords)   {
            $sql['_string']     =   "name like '%{$keywords}%'";
        }
        $data   =   D("Common")->getList('course',$sql,'',$page,10,'id desc');
        foreach($data['lists'] as $k=>$v){
            $data['lists'][$k]['day']   =  task_course_day($v['day']);
            $data['lists'][$k]['time']   =  task_course_time($v['time']);
            $data['lists'][$k]['statu']   =  task_course_status($v['status']);

            $num = M("elective")->where(array("course_id"=>$v['id']))->count();
            $data['lists'][$k]['num']   =  $num;
        }


        $this->assign("lists",$data['lists']);
        $this->assign("pages",$data['pages']);
        $this->assign("cate_id",$cate_id);
        $this->assign('keywords',$keywords);
        $this->display();
    }

    public function addCourse()
    {
        if($_POST){

            $data   =   I("post.");


            $data['status']  =  1;
            if($data['id']){
                // 编辑
                $res    =   M("course")->where(array("id"=>$data['id']))->save($data);
            } else {
                $res = M('course')->add($data);
            }
            if($res     !== false){
//                $this->redirect('添加成功',"Admin/Student/index");
                $this->success('操作成功',"/Admin/Course/index");
            }else $this->error("编辑失败");
        }else{
            $id =   I("get.id");
            if($id){
                $page   =   I("get.page");
                $info   =   M("course")->where(array("id"=>$id))->find();

                $this->assign("info",$info);
                $this->assign("page",$page);
            }

            $this->display();
        }
    }

    public function addTime()
    {
        if($_POST){

            $data   =   I("post.");

            $res    =   M("course")->where(array("id"=>$data['id']))->save($data);
            if($res     !== false){
                $this->success('操作成功',"/Admin/Course/index");
            }else $this->error("编辑失败");
        }else{
            $id =   I("get.id");
            if($id){
                $page   =   I("get.page");
                $info   =   M("course")->where(array("id"=>$id))->find();

                $this->assign("info",$info);
                $this->assign("page",$page);
            }
            $this->display();
        }
    }

    public function depClass () {
        $dep_id       =   I('get.depId');
        $data   =   M("class")->where(array("dep_id"=>$dep_id))->select();
        echo json_encode($data);
    }

    //修改数据
    public function startData()
    {
        $id     =   I('get.id');
        $table  =   I('get.table');
        $data   =   M($table)->where("id in ($id)")->find();
        $data['status'] = I('get.status');

        $res    =   M("course")->where(array("id"=>$data['id']))->save($data);
        echo $res;
    }
    //删除数据
    public function delData()
    {
        $id     =   I('get.id');
        $table  =   I('get.table');
        $info   =   M($table)->where("id in ($id)")->find();

        if ($info['status'] != 1) {
            $res = 2;
        } else {
            $res    =   M($table)->where("id in ($id)")->delete();
            if($res !== false){
                if($info['pic']) unlink('.'.$info['pic']);
                if($info['mini_pic']) unlink('.'.$info['mini_pic']);
            }
        }
        echo $res;
    }








    public function index1()
    {
        $page       =   I('get.page');
        $cate_id    =   I("get.cate_id");
        if(empty($page))    $page   =   0;


        $ids    =   D("Common")->getCateIds('pro_cate',$cate_id);
        $keywords   =   I("get.keywords");
        if($ids)    $sql    =   "cate_id in ({$ids})";
        else $sql   =   '';
        if($keywords)   {
            $sql['_string']     =   "u.name like '%{$keywords}%'";
        }

        $pages  =   getPage('users u',$sql,$page,10,5,"ab_department as d ON d.id = u.dep_id,ab_class as c ON c.id = u.class_id");
        $num    =   $pages['page']*10;

        $lists  =   M('users u')
            ->join("ab_department as d ON d.id = u.dep_id","left")
            ->join("ab_class as c ON c.id = u.class_id","left")
            ->where($sql)->order('u.id desc')->limit($num,10)
            ->field("u.id,u.student_id,u.pic,u.name,u.phone,u.add_time,d.name depName,c.name className")
            ->select();


        $this->assign("lists",$lists);
        $this->assign("pages",$pages);
        $this->assign("cate_id",$cate_id);
        $this->assign('keywords',$keywords);
        $this->display();
    }
}